import time
import datetime
import subprocess

# command = ['go', 'test', '-run', '2A']
# command = ['go', 'test', '-run', '2(A|B)']
# command = ['go', 'test', '-run', '2(A|B)', '-race']
# command = ['go', 'test', '-run', '^TestReliableChurn2C$']
command = ['go', 'test']
predicate = 'FAIL'

count = 0
while True:
    count += 1
    print(f'⇒ Running #{count}...')
    inst = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    out = inst.stdout
    summary = '\n'.join(out.decode('utf-8').split('\n')[-10:])
    print(summary)

    if summary.find(predicate) != -1:
        suffix = str(datetime.datetime.now()).replace(' ', '-')
        with open(f'log-{suffix}.txt', 'wb') as fp:
            fp.write(out)
        exit(0)

    time.sleep(1)